package com.pai4j.pgc.dao.community;
import com.pai4j.pgc.entity.community.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Repository
public interface ICoderUtilUserDAO extends JpaSpecificationExecutor<UserEntity>, JpaRepository<UserEntity, Long> {

    /***
     * 根据手机号查询用户信息
     * @param mobile
     * @return
     */
    UserEntity findByMobile(String mobile);
    List<UserEntity> findAllByUserIdIn(Collection<String> userIds);

    UserEntity findByWxOpenId(String wxOpenId);
    UserEntity findByWeixinOpenId(String weixinOpenId);

    UserEntity findByUserId(String userId);

    @Query(value = "select u.* from pai4j_user u where u.uid is not null order by u.id desc limit 100",
                    nativeQuery = true)
    List<UserEntity> findAllCpsUsersLimit100();

    Long countByCpsUid(String userId);

    @Query("select u.utmSource from UserEntity u")
    Set<String> findAllUtmSource();

    @Query(value = "select u from UserEntity u where u.userId = :userId Or u.utmSource = :utmSource")
    UserEntity findAllByUserIdOrUtmSource(String userId, String utmSource);

    List<UserEntity> findByUserIdIn(List<String> userId);

    /***
     * 获取用户注册的名字
     * @param userId
     * @return
     */
    @Query(value = "SELECT count(*) from (SELECT * FROM pai4j_user ORDER BY id) as a where id <= (SELECT id from pai4j_user where user_id = ?1)", nativeQuery = true)
    Long findUserRegistryIndex(String userId);

    @Query(value = "select distinct(user_id) from pai4j_user", nativeQuery = true)
    List<String> findAllUserName();

    List<UserEntity> findByUserName(String userName);

    /***
     * 根据手机号+密码查询用户信息，登录场景
     * @param mobile
     * @param password
     * @return
     */
    UserEntity findByMobileAndPassword(String mobile, String password);

    @Query(value = "select count(*) from pai4j_user t where t.create_date = now()", nativeQuery = true)
    Long todayCount();

    @Query(value = "select u from UserEntity u where (u.createDate between ?1 and ?2) or (u.updateDate between ?1 and ?2)")
    List<UserEntity> findAllByUpdateDateBetween(Date start, Date end);

    /**
     * 查询我的邀请记录
     * @param cpsUid
     * @param pageable
     * @return
     */
    Page<UserEntity> findAllByCpsUid(String cpsUid, Pageable pageable);

    @Query(value = "select u.user_id from pai4j_user u order by u.id asc limit 1000", nativeQuery = true)
    List<String> findTop1000UserId();

    @Query(value = "select distinct u.userId from UserEntity u where u.roleCode = :roleCode")
    Set<String> findAllUserIdByRoleCode(Integer roleCode);

    @Query(value = "select u.userId from UserEntity u where u.userName like ?1")
    List<String> searchAllByKeywords(String keyword, Pageable pageable);

    void deleteAllByWeixinOpenIdAndUserIdNot(String weixinOpenId, String userId);
    void deleteAllByMobileAndUserIdNot(String mobile, String userId);
}
